package app.base.root;

import java.io.IOException;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;

import app.base.commons.*;

public class HomePageIndex extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Constants.CHARSET_ENCODING_UTF8);
	    response.setContentType(Constants.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 

	    Constants.initByHttpServletRequest(request);
			    
		try {
			out.print(Header.renderTo(null));
			out.print(this.renderTo(null));
			out.print(Footer.renderTo(null));
		} catch (Throwable e) {
			e.printStackTrace();
		}
		
		out.flush();
		out.close();
	} 
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	public String renderTo(Map<String, Object> params) throws Throwable {
		VelocityContext context = new VelocityContext();
	
		context.put("APP_DOMAIN_NAME", Constants.APP_DOMAIN_NAME);
		context.put("SERVLET_CONTEXT_PATH", Constants.SERVLET_CONTEXT_PATH);
		context.put("VELOCITY_VM_PATH", Constants.VELOCITY_VM_PATH);
		context.put("HOOK_HOME", Constants.APP_DOMAIN_NAME);

		return Tools.velocityTranslate(
					Constants.VELOCITY_PROPERTIES_FILENAME, 
					context, 
					Constants.VELOCITY_TEMPLATES_THEMES_PATH_DEFAULT + "/index.vm"
				);		
	}

}
